<template>
  <div class="app-header-sticky" :class="{show: isSticky}" v-show="isSticky">
    <div class="container">
      <RouterLink class="logo" to="/" />
      <AppHeaderNav />
      <!-- 最右侧导航 -->
      <div class="right-nav">
        <transition :name="show ? 'fade-in1' : 'fade-in2'" mode="out-in">
          <div class="href" v-if="!show" key="1">
            <RouterLink to="/brand" >品牌</RouterLink>
            <RouterLink to="/special" >专题</RouterLink>
          </div>
          <div class="search" v-else key="2">
            <span @click="show=false">
              <i class="iconfont icon-angle-right"></i>
              <i class="iconfont icon-angle-right"></i>
            </span>
            <input v-model="keyword" @keyup.enter="toSearch()" type="text" placeholder="搜一搜">
          </div>
        </transition>
        <div class="icon">
          <a href="javascript:;" @click="show=true"><i class="iconfont icon-search"></i></a>
          <router-link to="/cart">
            <i class="iconfont icon-cart"></i>
            <em v-if="$store.getters['cart/validTotal']">{{$store.getters['cart/validTotal']}}</em>
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import AppHeaderNav from '@/components/app-header-nav.vue'
// import { useWindowScroll } from '@vueuse/core'
import { onMounted, ref } from 'vue-demi'
import { useRouter } from 'vue-router'
export default {
  name: 'AppHeaderSticky',
  components: {
    AppHeaderNav
  },
  setup () {
    onMounted(() => {
      window.addEventListener('scroll', () => {
        isSticky.value = document.documentElement.scrollTop >= 85
      }, false)
    })
    const isSticky = ref(false)
    const show = ref(false)
    const keyword = ref('')
    // const { y } = useWindowScroll()
    const router = useRouter()
    const toSearch = () => {
      router.push({ path: '/search', query: { keyword: keyword.value } })
      // 页面跳转之后将输入框内容置空
      keyword.value = ''
    }
    return { isSticky, show, keyword, toSearch }
  }
}
</script>

<style lang="less" scoped>
.app-header-sticky {
  width: 100%;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 999;
  background: #fff;
  border-bottom: 1px solid #e4e4e4;
  transform: translate3d(0, -100%, 0);
  opacity: 0;
  &.show {
    z-index: 999;
    transform: translateZ(0);
    opacity: 1;
    transition: all .2s linear;
  }
  .container {
    display: flex;
    align-items: center;
    position: relative;
  }
  .logo {
    width: 200px;
    height: 80px;
    text-indent: -999px;
    background-image: url(../assets/images/logo.png);
    background-size: 160px auto;
    background-repeat: no-repeat;
    background-position: center 5px;
  }
  .right-nav {
    width: 230px;
    display: flex;
    font-size: 16px;
    position: relative;
    line-height: 34px;
    align-items: center;
    .href {
      width: 152px;
      display: flex;
      a {
        // width: 38px;
        margin-right: 44px;
        // font-size: 16px;
        line-height: 30px;
        border-bottom: 2px solid transparent;
        margin-top: 2px;
        &:hover {
          color: @xtxColor;
          border-color: @xtxColor;
        }
      }
    }
    .search {
      width: 152px;
      display: flex;
      height: 34px;
      position: relative;
      align-items: center;
      > span {
        letter-spacing: -6px;
        width: 20px;
        position: absolute;
        left: -20px;
        top: 0;
        line-height: 32px;
        height: 32px;
        z-index: 999;
        &:hover {
          cursor: pointer;
          color: @xtxColor;
        }
        i {
          font-size: 12px;
        }
      }
      > input {
        width: 152px;
        border-bottom: 1px solid @xtxColor;
        border-top: 1px solid transparent;
        height: 26px;
        font-size: 14px;
      }
    }
    .icon {
      flex: 1;
      display: flex;
      > a {
        width: 39px;
        text-align: center;
        color: @xtxColor
      }
      em {
        font-style: normal;
        position: absolute;
        right: 0;
        top: 0;
        padding: 1px 6px;
        line-height: 1;
        background: @helpColor;
        color: #fff;
        font-size: 12px;
        border-radius: 10px;
        font-family: Arial;
      }
    }
  }
}
.fade-in1 {
  &-enter {
    opacity: 0;
    transform: translate3d(0,10px,0);
    &-active {
      transition: all .2s linear;
    }
    &-to {
      opacity: 1;
    }
  }
  &-leave {
    opacity: 1;
    &-active {
      transition: all .2s linear;
    }
    &-to {
      opacity: 0;
      transform: translate3d(0,-10px,0);
    }
  }
}
.fade-in2 {
  &-enter {
    opacity: 0;
    transform: translate3d(0,-10px,0);
    &-active {
      transition: all .2s linear;
    }
    &-to {
      opacity: 1;
    }
  }
  &-leave {
    opacity: 1;
    &-active {
      transition: all .2s linear;
    }
    &-to {
      opacity: 0;
      transform: translate3d(0,10px,0);
    }
  }
}
</style>
